package com.sx.pipeline.ui.wallet.recharge;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;

public class De {

    // Alipay configuration
    private static final String APP_ID = "your_app_id"; // Your Alipay App ID
    private static final String PRIVATE_KEY = "your_private_key"; // Your RSA private key (PKCS8 format)
    private static final String CHARSET = "utf-8";
    private static final String SIGN_TYPE = "RSA2"; // or "RSA"

    public static void main(String[] args) {
        try {
            String orderInfo = getOrderInfo("Order Title", "Order Description", "0.01", "out_trade_no_12345");
            String signedOrderInfo = signOrderInfo(orderInfo);
            System.out.println("Order Info: " + orderInfo);
            System.out.println("Signed Order Info: " + signedOrderInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getOrderInfo(String subject, String body, String totalAmount, String outTradeNo) {
        Map<String, String> params = new TreeMap<>();
        params.put("app_id", APP_ID);
        params.put("method", "alipay.trade.app.pay");
        params.put("format", "json");
        params.put("charset", CHARSET);
        params.put("sign_type", SIGN_TYPE);
        params.put("timestamp", "2024-08-05 12:00:00");
        params.put("version", "1.0");
        params.put("notify_url", "http://yourdomain.com/notify");
        params.put("biz_content", "{\"subject\":\"" + subject + "\",\"out_trade_no\":\"" + outTradeNo + "\",\"total_amount\":\"" + totalAmount + "\",\"body\":\"" + body + "\"}");

        StringBuilder orderInfo = new StringBuilder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (orderInfo.length() > 0) {
                orderInfo.append("&");
            }
            orderInfo.append(entry.getKey()).append("=").append(entry.getValue());
        }
        return orderInfo.toString();
    }

    public static String signOrderInfo(String orderInfo) throws Exception {
        PrivateKey privateKey = getPrivateKey(PRIVATE_KEY);
        java.security.Signature signature = java.security.Signature.getInstance("SHA256withRSA");
        signature.initSign(privateKey);
        signature.update(orderInfo.getBytes(CHARSET));
        byte[] signedData = signature.sign();
        return Base64.getEncoder().encodeToString(signedData);
    }

    public static PrivateKey getPrivateKey(String privateKeyStr) throws Exception {
        privateKeyStr = privateKeyStr.replaceAll("-----BEGIN PRIVATE KEY-----", "")
                .replaceAll("-----END PRIVATE KEY-----", "")
                .replaceAll("\n", "");
        byte[] keyBytes = Base64.getDecoder().decode(privateKeyStr);
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePrivate(spec);
    }
}
